<template>
  <el-dialog
    append-to-body
    :title="info"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    align="left"
    v-loading="currLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    width="600px">
    <el-form ref="form" v-if="dialogVisible" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="名称:" align="left" prop="name">
        <el-input v-model="form.name" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="类型:" align="left" prop="type">
        <el-select v-model="form.type">
          <el-option
            v-for="item in typeArr"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选项值:" align="left" prop="itemList" v-if="form.type===1">
        <product-specifications-box ref="product-specifications-box" :callback="setItemList"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import ProductSpecificationsBox from '../Common/ProductSpecificationsBox'
export default {
  name: 'CreateSupplierProductSpecificationsDialog',
  components: {ProductSpecificationsBox},
  props:{
    successFunction:Function
  },
  data: function () {
    return {
      info:'创建',
      dialogVisible: false,
      currLoading: false,
      typeArr:this.listToArray(dict.ShopProductSpecificationsTypeArr),
      form: {
        id:0,
        name: '',
        type: 0,
        itemList: '',
      },
      rules: {
        name: [
          {required: false, message: '请输入 名称', trigger: ['blur', 'change']},
          {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: ['blur', 'change']}
        ]
      }
    }
  },
  mounted () {
  },
  methods: {
    setItemList:function (itemList) {
      this.form.itemList=itemList
      this.$forceUpdate()
    },
    open: function (infoData) {
      let $this=this
      $this.info='创建规格'
      if (infoData.id) {
        $this.info='更新规格'
        $this.form.id = infoData.id
        $this.form.name = infoData.name
        $this.form.type = infoData.type.id
        $this.form.itemList = infoData.itemList
        $this.$forceUpdate()
        if ($this.form.type===1){
          setTimeout(function (){
            $this.form.itemList=$this.$refs['product-specifications-box'].setTag(infoData.itemList.split(','))
          },500)
        }
      }else{
        $this.form={
          id:0,
          name: '',
          type: 0,
          itemList: '',
        }
        if ($this.$refs.form){
          $this.$refs.form.resetFields()
        }
      }
      $this.dialogVisible = true
      $this.$forceUpdate()
    },
    submit: function () {
      let $this = this
      $this.$refs.form.validate((valid) => {
        if (valid) {
          var api = 'SupplierProductSpecifications/create'
          var form = Object.assign({}, $this.form)
          if (this.form.id > 0) {
            api = 'SupplierProductSpecifications/update'
          }
          if (form.type===1){
            form.itemList=form.itemList.split(',')
          }else{
            form.itemList=''
          }
          $this.currLoading = true
          $this.MyRequest(this.ApiDoNameSupplier + api, $this.form, function () {
            $this.$message.success($this.info+'成功')
            $this.currLoading = false
            $this.form = {
              id:0,
              name: '',
              type: 0,
              itemList: '',
            }
            $this.$props.successFunction()
            $this.dialogVisible=false
            $this.$forceUpdate()
          }, function (response) {
            $this.$message.error(response.message)
            $this.currLoading = false
            $this.$forceUpdate()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
